﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FortuneHeavenCommon.DataMap;
using System.Data.SqlClient;

namespace FortuneHeavan.Game.DAL
{
    public class RechargeHistoryDAL : UserDBBase
    {
        public List<RechargeHistory> GetList(int page, int size, string username)
        {
            if (string.IsNullOrEmpty(username) || username.Trim() == "")
            {
                string sql = @"SELECT * FROM (select A.*,B.GameID,B.Accounts as UserName ,ROW_NUMBER() OVER(ORDER BY A.ID DESC) ROW   
from  dbo.RechargeHistory A  left join dbo.AccountsInfo B  on A.UserID = B.UserID
 ) FF
 WHERE FF.ROW BETWEEN  @p0 AND @p1";
                List<RechargeHistory> list = Query<RechargeHistory>(sql, new SqlParameter("@p0", size * (page - 1) + 1), new SqlParameter("@p1", size * page));
                return list;
            }
            else
            {
                string sql = @"SELECT * FROM (select A.*,B.GameID,B.Accounts as UserName,ROW_NUMBER() OVER(ORDER BY A.ID DESC) ROW   
from   dbo.RechargeHistory A  left join dbo.AccountsInfo B  on A.UserID = B.UserID WHERE B.Accounts = @usrname
 ) FF
 WHERE FF.ROW BETWEEN  @p0 AND @p1";
                List<RechargeHistory> list = Query<RechargeHistory>(sql, new SqlParameter("@usrname", username), new SqlParameter("@p0", size * (page - 1) + 1), new SqlParameter("@p1", size * page));
                return list;
            }
        }
        public int GetTotalSize(string username)
        {
            if (!string.IsNullOrEmpty(username) && username.Trim() != "")
            {
                string sql = @"select count(A.id) from   dbo.RechargeHistory A  left join dbo.AccountsInfo B  on A.UserID = B.UserID  where B.Accounts =@p0";
                return RunSQL(sql, new SqlParameter("@p0", username));
            }
            else
            {
                string sql = @"select count(id) from RechargeHistory";
                return RunSQL(sql);
            }
        }

        public List<RechargeHistory> GetListByUser(int page, int size, int userid)
        {
        
                string sql = @"SELECT * FROM (select *,ROW_NUMBER() OVER(ORDER BY ID DESC) ROW   
from   dbo.RechargeHistory  WHERE UserID = @UserID
 ) FF
 WHERE FF.ROW BETWEEN  @p0 AND @p1";
                List<RechargeHistory> list = Query<RechargeHistory>(sql, new SqlParameter("@UserID", userid), new SqlParameter("@p0", size * (page - 1) + 1), new SqlParameter("@p1", size * page));
                return list;
            
        }
        public int GetTotalSizeByUser(int userid)
        {
            string sql = @"select count(id) from   dbo.RechargeHistory   where UserID =@UserID";
            return RunSQL(sql, new SqlParameter("@UserID", userid));
        }

        public RechargeHistory GetChargeByCode(string code)
        {
            string sql = @"select * from   dbo.RechargeHistory   where CardCode =@code";
            List<RechargeHistory> list = Query<RechargeHistory>(sql, new SqlParameter("@code", code));
            if (list != null && list.Count > 0)
            {
                return list[0];
            }
            return null;
        }

        public RechargeHistory GetChargeByID(int id)
        {
            string sql = @"select * from   dbo.RechargeHistory   where ID =@ID";
            List<RechargeHistory> list = Query<RechargeHistory>(sql, new SqlParameter("@ID", id));
            if (list != null && list.Count > 0)
            {
                return list[0];
            }
            return null;
        }
    }
}
